package com.ocp.macro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocp.macro.entity.AlctProduct;
import com.ocp.macro.entity.Allocation;
import com.ocp.macro.entity.DealerAccount;
import com.ocp.macro.enums.DataCode;
import com.ocp.macro.mapper.allocation.AllocationMapper;
import com.ocp.macro.mapper.allocation.AllocationShowMapper;
import com.ocp.macro.service.AllocationShowService;
import com.ocp.macro.utils.GsonUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lhd
 * @version 1.0
 * @date 2021/10/9 16:26
 * 动态显示调拨单
 */
@SuppressWarnings("all")
@Service
@Transactional(rollbackFor = Exception.class)
public class AllocationShowServiceImpl implements AllocationShowService {
    @Resource
    private AllocationShowMapper allocationShowMapper;
    @Resource
    private AllocationMapper allocationMapper;

    /**
     * 动态查询调拨单详细信息
     * @param allocation 动态查询参数
     * @return json 调拨单结果
     */
    @Cacheable(cacheNames = "allocations",key = "#allocation")
    @Override
    public String  showConditionAllocation(Allocation allocation) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(allocation.getPage(), allocation.getRows());
        List<Allocation> allocations=allocationShowMapper.conditionAllocation(allocation);
        PageInfo<Allocation> pageInfo = new PageInfo <>( allocations );
        map.put("data",pageInfo.getList());
        pageInfo.setList(null);
        map.put("pageInfo",pageInfo);
        map.put("code", DataCode.SUCCESS.getCode());
        map.put("message",DataCode.SUCCESS.getMessage());
        return GsonUtils.toJson(map);
    }

    /**
     * 调拨单中调拨产品信息
     * @param alctNo 调拨单编号
     * @param page 当前页码
     * @return json
     */
    @Cacheable(cacheNames = "allct")
    @Override
    public String showConditionAlctProduct(String alctNo,Integer page) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, 10);
        List<AlctProduct> alctProducts= allocationShowMapper.conditionAlctProduct(alctNo);
        PageInfo<AlctProduct> pageInfo = new PageInfo <>( alctProducts );
        map.put("data",pageInfo.getList());
        pageInfo.setList(null);
        map.put("pageInfo",pageInfo);
        map.put("code", DataCode.SUCCESS.getCode());
        map.put("message",DataCode.SUCCESS.getMessage());
        return GsonUtils.toJson(map);
    }
    @Cacheable(cacheNames = "scao")
    @Override
    public String showConditionAllocationOne(String alctNo) {
        Map<String, Object> map = new HashMap<>();
        Allocation allocation = new Allocation();
        allocation.setAlctNo(alctNo);
        List<Allocation> allocations=allocationShowMapper.conditionAllocation(allocation);
        map.put("data",allocations);
        map.put("addr",allocationMapper.selectAddressById(allocations.get(0).getReceiveAddrId().intValue()));
        map.put("code", DataCode.SUCCESS.getCode());
        map.put("message",DataCode.SUCCESS.getMessage());
        return GsonUtils.toJson(map);
    }
}
